package ClientUI;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.BorderFactory;

import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import Protocol.contact.Contact;
import Protocol.contact.ContactGroup;
import Protocol.contact.GmRecord;
import Protocol.noticeMsgWord.NoticeMsgWord_R;
import Protocol.user.BasePersonInfo;
import java.awt.BorderLayout;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ContactJPanel extends javax.swing.JPanel {
	private JTabbedPane jTabbedPane1;
	private JPanel jPanel1;
	private JScrollPane jScrollPane1;
	private JScrollPane jScrollPane2;
	private JScrollPane jScrollPane3;
	private JTree jTree3;
	private JTree jTree2;
	private JTree jTree1;
	private JPanel jPanel3;
	private JPanel jPanel2;
	
	private ContactJDialog   myDialog;
	private ClientGUI		 clientGui;
	private Contact			myContact;
	private ContactJDialog	 currentChatDialog;
	private NoticeMsgWord_R		 currentMsg_R;
	private BasePersonInfo	 currentTargetBpi;
	private ArrayList<ContactJDialog>     chatDialogList = new ArrayList<ContactJDialog>();
	
	public Contact getMyContact() {
		return myContact;
	}

	public void setMyContact(Contact myContact) {
		this.myContact = myContact;
	}
	BasePersonInfo	selectedBaseInfo = new BasePersonInfo();
	
	

	public BasePersonInfo getSelectedBaseInfo() {
		return selectedBaseInfo;
	}

	public void setSelectedBaseInfo(BasePersonInfo selectedBaseInfo) {
		this.selectedBaseInfo = selectedBaseInfo;
	}

	public ArrayList<ContactJDialog> getChatDialogList() {
		return chatDialogList;
	}

	public void setChatDialogList(ArrayList<ContactJDialog> chatDialogList) {
		this.chatDialogList = chatDialogList;
	}

	public ContactJDialog getCurrentChatDialog() {
		return currentChatDialog;
	}

	public void setCurrentChatDialog(ContactJDialog currentChatDialog) {
		this.currentChatDialog = currentChatDialog;
	}

	public NoticeMsgWord_R getCurrentMsg_R() {
		return currentMsg_R;
	}

	public void setCurrentMsg_R(NoticeMsgWord_R currentMsg_R) {
		this.currentMsg_R = currentMsg_R;
	}

	public BasePersonInfo getCurrentTargetBpi() {
		return currentTargetBpi;
	}

	public void setCurrentTargetBpi(BasePersonInfo currentTargetBpi) {
		this.currentTargetBpi = currentTargetBpi;
	}
	
	
	class MyJTree extends JTree implements ActionListener {
		JPopupMenu  popup = new JPopupMenu();
        JMenuItem   mi = new JMenuItem("show detail");
        DefaultMutableTreeNode   roo;
        
        MyJTree(DefaultMutableTreeNode root) {
        	super(root);
        	roo = root;
        	mi.addActionListener(this);
        	mi.setActionCommand("detail");
        	popup.add(mi);
        	/*mi = new JMenuItem("chat");
        	mi.addActionListener(this);
        	mi.setActionCommand("chat");
        	popup.add(mi);*/
        	this.addMouseListener(new MouseAdapter() {
        		public void mouseClicked(MouseEvent e) {
        			handleDoubleClicked(e);
        		}
        		public void mouseReleased(MouseEvent e) {
        			if(e.isPopupTrigger())
        			popup.show((JComponent)e.getSource(), e.getX(), e.getY());
        		}
        	});
        }
        private void handleDoubleClicked(MouseEvent e) {
    		int   buttonMask = e.getButton(),	clickCount = e.getClickCount();
    		if(buttonMask == e.BUTTON1 && clickCount == 2) {
    			if(((DefaultMutableTreeNode)((JTree)e.getSource()).getSelectionPath().getLastPathComponent()).isLeaf()) {
    				Dimension	screen = Toolkit.getDefaultToolkit().getScreenSize();
    				
    				DefaultMutableTreeNode  dmtn;
    				dmtn = (DefaultMutableTreeNode)((JTree)e.getSource()).getSelectionPath().getLastPathComponent();
    				
    				myDialog = new ContactJDialog(null, clientGui, (BasePersonInfo)dmtn.getUserObject());
    				myDialog.setSize(470, 620);
    				myDialog.setLocation(screen.width/2-myDialog.getWidth()/2, screen.height/2-myDialog.getHeight()/2);
    				myDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);        				
    				myDialog.setVisible(true);
    				chatDialogList.add(myDialog);
    			}		
    		}
    	}
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			DefaultMutableTreeNode  dmtn, node;
			TreePath  path = this.getSelectionPath();
			dmtn = (DefaultMutableTreeNode) path.getLastPathComponent();
			if(e.getActionCommand().equalsIgnoreCase("detail")) {
				clientGui.mainFrame.updateFeedBackPanel((BasePersonInfo)dmtn.getUserObject());
				/*node = new DefaultMutableTreeNode("child");
				dmtn.add(node);
				((DefaultTreeModel) this.getModel()).nodeStructureChanged((TreeNode)dmtn);*/
			}
			if(e.getActionCommand().equalsIgnoreCase("chat")) {
				/*Dimension	screen = Toolkit.getDefaultToolkit().getScreenSize();
				
				DefaultMutableTreeNode  dmn = null;
				//dmtn = (DefaultMutableTreeNode)((JTree)e.getSource()).getSelectionPath().getLastPathComponent();
				
				myDialog = new ContactJDialog(null, clientGui, (BasePersonInfo)dmn.getUserObject());
				myDialog.setSize(470, 620);
				myDialog.setLocation(screen.width/2-myDialog.getWidth()/2, screen.height/2-myDialog.getHeight()/2);
				myDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);        				
				myDialog.setVisible(true);
				chatDialogList.add(myDialog);*/
			}
		}
		
	}

	
	public ContactJPanel(ClientGUI    cgui, Contact  initContact) {
		super();
		clientGui = cgui;
		myContact = initContact;
		initGUI();
	}
	public void updateContact(Contact  contact) {
		this.myContact = contact;
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				displayContact();
				jPanel1.repaint();
			}
			
		});
		
	}
	private void displayContact() {
		jScrollPane3 = new JScrollPane();
		jPanel1.add(jScrollPane3);
		jScrollPane3.setPreferredSize(new java.awt.Dimension(220, 360));
		{
			DefaultMutableTreeNode   root = new DefaultMutableTreeNode("");
			DefaultMutableTreeNode tempGroupNode = null, tempMemberNode = null;
			for(int i=0; i<myContact.getGroupList().size(); i++) {
				tempGroupNode = new DefaultMutableTreeNode(myContact.getGroupList().get(i));    //group node
				
				for(int j=0; j<myContact.getGroupList().get(i).getMemberIdList().size(); j++) {
					GmRecord    record = myContact.getGroupList().get(i).getMemberIdList().get(j);
					BasePersonInfo  bpi = this.clientGui.getClient().getClientData().getBpi(record.getMember_id());
					tempMemberNode = new DefaultMutableTreeNode(bpi);
					tempGroupNode.insert(tempMemberNode, j);
					tempMemberNode = null;
				}
				root.insert(tempGroupNode, i);
				tempGroupNode = null;
			}		
			jTree1 = new MyJTree(root);
			jTree1.setRootVisible(false);
			jScrollPane3.setViewportView(jTree1);
			jTree1.setPreferredSize(new java.awt.Dimension(159, 306));
		}
	}
	public void appendChatMsg(NoticeMsgWord_R  cmr) {
		
		for(int j=0; j<this.chatDialogList.size(); j++) {
			if(chatDialogList.get(j).getTargetBpi().getUser_id() == cmr.getChatMsgList().get(0).getFrom_id()) {
				chatDialogList.get(j).setCurrentMsg(cmr);
				break;
			}
		}
	}
	public void addNewNotice(NoticeMsgWord_R   cmr) {
		currentMsg_R = cmr;
		/*for(int i=0; i<myContact.getGroupList().size(); i++) {
			boolean isBreak = false;*/
		this.selectedBaseInfo = this.clientGui.getClient().getClientData().getBpi(cmr.getChatMsgList().get(0).getFrom_id());
			/*for(int j=0; j<myContact.getGroupList().get(i).getMemberIdList().size(); j++) {
				if(myContact.getGroupList().get(i).getMemberIdList().get(j).getMember_id() == cmr.getChatMsgList().get(0).getFrom_id()) {
					this.selectedBaseInfo = myContact.getGroupList().get(i).getGroupMemberList().get(j);
					isBreak = true;
					break;
				}
			}
			if(isBreak)  break;*/
		//}	
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				Dimension	screen = Toolkit.getDefaultToolkit().getScreenSize();
					
				myDialog = new ContactJDialog(null, clientGui, selectedBaseInfo);
				myDialog.setSize(470, 620);
				myDialog.setLocation(screen.width/2-myDialog.getWidth()/2, screen.height/2-myDialog.getHeight()/2);
				myDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);        				
				myDialog.setVisible(true);
				myDialog.setCurrentMsg(currentMsg_R);
				chatDialogList.add(myDialog);
			}
		});
		
	}
	private void initGUI() {
		try {
			{
				//this.setPreferredSize(new java.awt.Dimension(206, 395));
				BorderLayout thisLayout = new BorderLayout();
				this.setLayout(thisLayout);
				this.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
			}
			//setPreferredSize(new Dimension(250, 400));
			{
				jTabbedPane1 = new JTabbedPane();
				this.add(jTabbedPane1, BorderLayout.CENTER);
				jTabbedPane1.setPreferredSize(new java.awt.Dimension(182, 332));
				{
					jPanel1 = new JPanel();
					jTabbedPane1.addTab("User", null, jPanel1, null);
					jPanel1.setPreferredSize(new java.awt.Dimension(245, 303));
					{
						if(myContact != null)   displayContact();   
					}
				}
				{
					jPanel3 = new JPanel();
					jTabbedPane1.addTab("Disc", null, jPanel3, null);
					jPanel3.setPreferredSize(new java.awt.Dimension(170, 250));
					{
						jScrollPane2 = new JScrollPane();
						jPanel3.add(jScrollPane2);
						jScrollPane2.setPreferredSize(new java.awt.Dimension(180, 352));
						{
							 DefaultMutableTreeNode root = new DefaultMutableTreeNode("groups");
							    DefaultMutableTreeNode mercury = new DefaultMutableTreeNode("disc1");
							    root.insert(mercury, 0);
							    DefaultMutableTreeNode venus = new DefaultMutableTreeNode("disc2");
							    root.insert(venus, 1);
							    DefaultMutableTreeNode mars = new DefaultMutableTreeNode("disc3");
							    root.insert(mars, 2);
							    DefaultMutableTreeNode class4 = new DefaultMutableTreeNode("disc4");
							    root.insert(class4, 3);
							jTree3= new MyJTree(root);
							jScrollPane2.setViewportView(jTree3);
							jTree3.setPreferredSize(new java.awt.Dimension(160, 317));
						}
					}
				}
				{
					jPanel2 = new JPanel();
					jTabbedPane1.addTab("Pros", null, jPanel2, null);
					{
						jScrollPane1 = new JScrollPane();
						jPanel2.add(jScrollPane1);
						jScrollPane1.setPreferredSize(new java.awt.Dimension(182, 350));
						{
							DefaultMutableTreeNode root = new DefaultMutableTreeNode("projects");
							DefaultMutableTreeNode mercury = new DefaultMutableTreeNode("pro1");
							root.insert(mercury, 0);
							DefaultMutableTreeNode venus = new DefaultMutableTreeNode("pro2");
							root.insert(venus, 1);
							DefaultMutableTreeNode mars = new DefaultMutableTreeNode("pro3");
							root.insert(mars, 2);
							DefaultMutableTreeNode class4 = new DefaultMutableTreeNode("pro4");
							root.insert(class4, 3);
							jTree2 = new MyJTree(root);
							jScrollPane1.setViewportView(jTree2);
							jTree2.setPreferredSize(new java.awt.Dimension(169, 316));
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
